package 蓝桥杯算法;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class day07 {
    public static void main(String[] args) {
       day07 a=new day07();
        String s="asdghbjjn";
        System.out.println(a.reserve(s,2));

    }

    public List<List<Integer>> threeSum(int[] nums) {//三数之和
        List<List<Integer>> result = new ArrayList<>();//
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                return result;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {//去重，并且不能与后面的比较，可能出现丢失结果
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if ((nums[left] + nums[right] + nums[i]) > 0) {
                    right--;
                } else if ((nums[left] + nums[right] + nums[i]) < 0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));//
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    right--;
                    left++;
                }
            }
        }
        return result;
    }

   /* public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int k = 0; k < nums.length; k++) {
            if (nums[k] >= 0 && nums[k] > target) {
                break;//减少任务量
            }
            if (nums[k] > 0 && nums[k] == nums[k - 1]) {
                continue;//去重
            }
            for (int i = k + 1; i > nums.length; i++) {
                if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {
                    break;
                }
                if (i > k + 1 && nums[i] == nums[i - 1]) {//去重
                    continue;
                }
                int left = i + 1;
                int right = nums.length - 1;
                while (right > left) {
                    long sum = nums[left] + nums[right] + nums[i] + nums[k];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[left], nums[right], nums[k]));
                        while (right > left && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        right--;
                        left++;
                    }
                }

            }
        }
        return result;*/
  /*  }*/
  /*  public void reverseString(char[] s){
        int l=0;
        int r=s.length;
        while(l<r){
            char temp=s[l];
            s[l]=s[r];
            s[r]=temp;
            l++;
            r--;
        }
    }*/
    public void reserve1(char[] ch,int i,int j){
       while(i<j){
           char temp=ch[i];
           ch[i]=ch[j];
           ch[j]=temp;
           i++;
           j--;
       }
    }
    public String reserve(String s,int k){
        char[] ch=s.toCharArray();
        for(int i=0;i<ch.length;i+=2*k){
            if(i+k<=ch.length){
                reserve1(ch,i,i+k-1);//注意这里是索引
                continue;
            }
            reserve1(ch,i,ch.length-1);//用于最后一个的反转，这里也是索引
        }
       return new String(ch);
    }
}

